package com.gitee.elf.core.data

import com.google.gson.*
import org.bukkit.DyeColor
import taboolib.library.configuration.ConfigurationSection
import java.lang.reflect.Type

class DataDyeColor(val value: DyeColor) : Data {

    override fun getValue(): Any {
        return value
    }
    override fun clone(): Data {
        return DataDyeColor(value)
    }

    companion object {


        fun ConfigurationSection.getDyeColor(it: String): DyeColor {
            return this.getConfigurationSection(it)!!.toDyeColor()
        }

        fun ConfigurationSection.toDyeColor(): DyeColor {
            return DyeColor.valueOf(getString("value","RED")!!.uppercase())
        }

    }

    @SerializerType(DyeColor::class)
    class Serializer : JsonSerializer<DataDyeColor>, JsonDeserializer<DataDyeColor> {

        override fun serialize(src: DataDyeColor?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
            return JsonPrimitive(src!!.value.name)
        }

        override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): DataDyeColor {
            return DataDyeColor(DyeColor.valueOf(json!!.asString))
        }


    }
}